package cc.fs.video.myvideoproject.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Environment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import cc.fs.video.myvideoproject.R
import cc.fs.video.myvideoproject.beans.FileBean
import cc.fs.video.myvideoproject.ui.adapter.FileAdapter
import cc.fs.video.myvideoproject.utils.bindView
import java.io.File
import java.util.*

/**
 * Created by fostion on 2015/10/14.
 */
class FileActivity : BaseActivity() {

    val toolbar: Toolbar by bindView(R.id.toolbar)
    val textPath: TextView by bindView(R.id.textPath)
    val recycleView: RecyclerView by bindView(R.id.recycleview)
    val layoutManager:RecyclerView.LayoutManager = LinearLayoutManager(this)
    var adapter: FileAdapter ?= null
    val list:ArrayList<FileBean> = ArrayList();
    val rootPath = Environment.getExternalStorageDirectory().toString()

    companion object{
        fun start(context: Activity,flag:Int){
            val intent = Intent(context,FileActivity::class .java)
            context.startActivityForResult(intent,flag)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_file)
        toolbar.title = "选择文件"
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)
        setSupportActionBar(toolbar)

        getFileData(rootPath)
        setUpRecycleView()
    }

    fun setUpRecycleView(){
        adapter = FileAdapter(list)
        recycleView.layoutManager = layoutManager
        recycleView.adapter = adapter

        adapter?.onClick = {
            file -> getFileData(file.path!!)
                    adapter?.notifyDataSetChanged()
        }
    }

    fun getFileData(path:String){
        list.clear()
        textPath.text = path
        val file:File = File(path)
        val childFiles = file.listFiles()
        for(file in childFiles){
            val fileBean = FileBean()
            fileBean.name = file.name
            fileBean.path = file.absolutePath
            if(file.isDirectory){
                fileBean.category = "dir"
            }else{
                fileBean.category = "file"
            }
            list.add(fileBean)
        }
    }

    override fun onBackPressed() {
        if(textPath.text.equals(rootPath)){
            notifyDone()
        } else {
            val file:File = File(textPath.text.toString())
            getFileData(file.parentFile.absolutePath)
            adapter?.notifyDataSetChanged()
        }
    }

    var doneMenuItem:MenuItem ?= null;
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
//        menuInflater.inflate(R.menu.menu_file, menu)
//        doneMenuItem = menu.findItem(R.id.action_done)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.action_done) {
            finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    fun notifyDone(){
        setResult(Activity.RESULT_OK,Intent())
        finish()
    }
}